package com.atguigu.serviceedu.controller;


import com.atguigu.commonutils.R;
import com.atguigu.commonutils.vo.CommonCourseInfoVo;
import com.atguigu.serviceedu.entity.EduCourse;
import com.atguigu.serviceedu.entity.coursevo.CourseInfoVo;
import com.atguigu.serviceedu.entity.coursevo.CoursePublish;
import com.atguigu.serviceedu.entity.coursevo.CourseQuery;
import com.atguigu.serviceedu.service.EduCourseService;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.swagger.annotations.Api;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * <p>
 * 课程 前端控制器
 * </p>
 *
 * @author xuan
 * @since 2020-04-14
 */
@Api(description="课程管理")
//@CrossOrigin
@RestController
@RequestMapping("edu/course")
public class EduCourseController {

    @Autowired
    EduCourseService eduCourseService;

    @PostMapping("addcourse")
    public R addCourse(@RequestBody CourseInfoVo courseInfo){
        String courseId = eduCourseService.editCourse(courseInfo);
        return R.ok().data("courseId",courseId);
    }

    @PutMapping("updatecourse")
    public R updateCourse(@RequestBody CourseInfoVo courseInfo){
        String courseId = eduCourseService.editCourse(courseInfo);
        return R.ok().data("courseId",courseId);
    }

    @PutMapping("publish/{courseId}")
    public R publish(@PathVariable("courseId") String courseId){
        boolean status = eduCourseService.publish(courseId);
        return status?R.ok():R.error();
    }

    @GetMapping("getcourse/{courseId}")
    public R getCourse(@PathVariable("courseId") String courseId){
        CourseInfoVo courseInfo = eduCourseService.getCourseInfoVo(courseId);
        return R.ok().data("courseInfo",courseInfo);
    }

    @GetMapping("getCoursePublish/{courseId}")
    public R getCoursePublish(@PathVariable("courseId") String courseId){
        CoursePublish coursePublish = eduCourseService.getCoursePublishById(courseId);
        return R.ok().data("coursePublish",coursePublish);
    }

    @DeleteMapping("{courseId}")
    public R deleteCourse(@PathVariable("courseId") String courseId){
        boolean status = eduCourseService.deleteCourse(courseId);
        return status?R.ok():R.error();
    }

    @PostMapping("pageQueryList/{page}/{limit}")
    public R getPageQueryList(@PathVariable("page") long currentPage,
                              @PathVariable("limit") long limit,
                              @RequestBody(required = false) CourseQuery searchObj){
        Page<EduCourse> page = new Page<>(currentPage, limit);
        QueryWrapper<EduCourse> wrapper = new QueryWrapper<>();
        if(!StringUtils.isEmpty(searchObj.getCourseName())){
            wrapper.like("title",searchObj.getCourseName());
        }
        if(!StringUtils.isEmpty(searchObj.getStatus())){
            wrapper.eq("status",searchObj.getStatus());
        }
        if(!StringUtils.isEmpty(searchObj.getSubjectId())){
            wrapper.eq("subject_id",searchObj.getSubjectId());
        }
        if(!StringUtils.isEmpty(searchObj.getSubjectParentId())){
            wrapper.eq("subject_Parent_id",searchObj.getSubjectParentId());
        }
        if(!StringUtils.isEmpty(searchObj.getTeacherId())){
            wrapper.eq("teacher_id",searchObj.getTeacherId());
        }
         eduCourseService.page(page, wrapper);
        long total = page.getTotal();
        List<EduCourse> records = page.getRecords();
        return R.ok().data("total",total).data("rows",records);
    }

    //根据课程id查询课程信息
    @GetMapping("getDto/{courseId}")
    public CommonCourseInfoVo getCourseInfoDto(@PathVariable String courseId) {
        CourseInfoVo courseInfoForm = eduCourseService.getCourseInfoVo(courseId);
        CommonCourseInfoVo courseInfo = new CommonCourseInfoVo();
        BeanUtils.copyProperties(courseInfoForm,courseInfo);
        return courseInfo;
    }

}

